﻿using System;
using System.Threading.Tasks;
using UnityEngine;
//using UnityEngine.AddressableAssets;
using UnityEngine.EventSystems;
using UnityEngine.Experimental.Rendering;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using Doozy.Engine.UI;
using System.Collections.Generic;
using Doozy.Engine;

namespace UnityEngine.UnityVideoPlayer
{
    /// <summary>
    /// Render to texture scene template.
    /// </summary>
    public abstract class AbstractRTTTemplate : AbstractTemplate
    {
        protected RenderTexture renderTarget;

        protected RenderTexture fsRenderTarget;

        protected RawImage viewPortCache;

        public override bool EnableSwitchFSMode => true;

        public void PostUnload(AbstractTemplate newTempate)
        {
            if (renderTarget != null)
            {
                RenderTexture.ReleaseTemporary(renderTarget);

                renderTarget = null;
            }

            if(fsRenderTarget != null)
            {
                RenderTexture.ReleaseTemporary(fsRenderTarget);

                fsRenderTarget = null;
            }
        }
        /// <summary>
        /// 渲染贴图的替换
        /// </summary>
        /// <param name="renderTexture"></param>
        public void ViewPortRenderTextureSwitch(RenderTexture renderTexture)
        {
            viewPortCache.texture = renderTexture;
        }

        public async  Task Load(RectTransform contentContainer, RectTransform exUiContainer, string customDatas)
        {
            //生成rt
            renderTarget = RenderTexture.GetTemporary((int)contentContainer.rect.width, (int)contentContainer.rect.height, 24, GraphicsFormat.R8G8B8A8_UNorm);

            SetupRT(renderTarget);

            //TODO 应该是必须延迟一帧
            await Task.Delay(100);

            await base.Load(contentContainer, exUiContainer, customDatas);

            //viewPortCache = content.Find<RawImage>("ViewPort");

            //SetupViewPort(viewPortCache);

            //if (!EnableSwitchFSMode) return;
            //if (fsBtnGroupCache == null)
            //{
            //    fsBtnGroupCache = content.Find<ContentGroup>("MaxButtonGroup");

            //    if (fsBtnGroupCache != null)
            //        SetupFSButtonGroups(fsBtnGroupCache);
            //}
        }

        //protected virtual void SetupFSButtonGroups(ContentGroup fsBtnGroup)
        //{
        //    if (!EnableSwitchFSMode)
        //    {
        //        fsBtnGroup.gameObject.SetActive(false);

        //        return;
        //    }

        //    fsBtnGroup.ActiveContent("MaxButton");

        //    fsBtnGroup.transform.Find<Button>("MaxButton").onClick.AddListener(() =>
        //    {
        //        Message.Send(new CtrTemplateFullScreenMessage() { fullScreenMode = true });

        //        fsBtnGroup.ActiveContent("MinButton");
        //    });

        //    fsBtnGroup.transform.Find<Button>("MinButton").onClick.AddListener(() => {
        //        Message.Send(new CtrTemplateFullScreenMessage() { fullScreenMode = false });

        //        fsBtnGroup.ActiveContent("MaxButton");
        //    });
        //}
         

        public override void FullScreenInFront(RectTransform fsContentContainer, RectTransform fsExUIContainer)
        {
            base.FullScreenInFront(fsContentContainer, fsExUIContainer);
        }

        public override void FullScreenInBehind(RectTransform contentContainer, RectTransform fsExUIContainer)
        {
            //卸载开始时将会将targetTexture置空  不允许全屏切换
            //if (rttSubSceneCtr.RenderCam.targetTexture == null)
            //    return;

            base.FullScreenInBehind(contentContainer, fsExUIContainer);

            //Debug.Assert(fsRenderTarget == null);

            fsRenderTarget = RenderTexture.GetTemporary((int)viewPortCache.rectTransform.rect.width, (int)viewPortCache.rectTransform.rect.height, 24, GraphicsFormat.R8G8B8A8_UNorm);

            SetupRT(fsRenderTarget);

            SetupViewPort(viewPortCache);

        }

        public override void NonFullScreen(RectTransform nonfsContentContainer, RectTransform exUIContainer)
        {
            base.NonFullScreen(nonfsContentContainer, exUIContainer);
        }
    }
}
